<?php

namespace App\Http\Controllers\Order;

use App\Helpers\ResponseData;
use App\Http\Controllers\Controller;
use App\Models\Order\Entrymanage;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use App\Models\Car\ExternalTransfer;
use Modules\Car\Entities\CarOrder;

class EntrymanageController extends Controller
{
    use ResponseData;
    public function index(Entrymanage $model,Request $request) {
        $size = $request->input('size',10);
        if($request->filled('department')){
            $model = $model->whereIn('department',$request->department);
        }
        if($request->filled('order_sn')){
            $model = $model->where('order_sn','like','%'.$request->order_sn.'%');
        }
        if($request->filled('start')&&$request->filled('end')){
            $model = $model->whereBetween('created_at',[$request->start,$request->end]);
        }
        $model2 = clone $model;
        $model3 = clone $model;
        // 计算合并后的数据的总金额
        $amount_total = $model2->whereNull('deleted_at')->where('status', 2)->sum('total_money');
        $amount_total_rz = $model3->whereNull('deleted_at')->where('status', 3)->sum('total_money');
        $data = $model->whereNull('deleted_at')->paginate($size);
        return $this->success(compact('data','amount_total','amount_total_rz'));
        // return $this->success($data);
    }
    // 统计金额
    public function getTotal(Entrymanage $model,Request $request){
        $model = $model->query()->selectRaw("SUM(transfer_price) as transfer_count,SUM(lease_price) as lease_count");
        if($request->filled('department')){
            $model = $model->where('department',$request->department);
        }
        $data = $model->first();
        return $this->success($data);
    }
    public function store(Entrymanage $model,Request $request){
        $data = $request->all();
        $data['order_sn'] = $this->getSn();
        $model->fill($data);
        $model->save();
        return $this->success('保存成功');
    }
    //设置订单号
    private function getSn(){
        $car = Entrymanage::query()
            ->select('id')
            ->where('id','>',0)
            ->orderByDesc('id')
            ->first();
        if($car){
            $num = $car->id+1;
        }else{
            $num = 1;
        }
        return 'wd'.date('Ymd',time()).$num;
    }
    public function edit(Entrymanage $model,Request $request){
        if($request->filled('order_sn')){
            $data = $model->where('order_sn',$request->order_sn)->first();
        } else {
            $data = $model->find($request->id);
        }
        return $this->success($data);
    }
    public function update(Entrymanage $model,Request $request){
        $data = $model->find($request->id);
        if($data){
            $data->update($request->all());
        }
        return $this->success('修改成功');
    }
    public function confirmRz(Entrymanage $model,Request $request){
        $is_wd = $request->is_wd;
        $order_sns = $request->order_sns;
        // 还原原订单为待入账
        if($is_wd == 2){
            ExternalTransfer::query()->whereIn('order_sn', $order_sns)->update(['is_reconciliation'=>3,'ok_reconciliation_time'=>now()]);
        }else{
            CarOrder::query()->whereIn('order_sn', $order_sns)->update(['is_reconciliation'=>3,'ok_reconciliation_time'=>now()]);
        }
        if($request->filled('id')){
            $data = $model->find($request->id);
            $data->confirm_at = now();
            $data->status = 3;
            $data->save();
        }
        return $this->success('入账成功');
    }
    public function destory(Entrymanage $model,Request $request){
        $is_wd = $request->is_wd;
        $order_sns = $request->order_sns;
        // 还原原订单为待入账
        if($is_wd == 2){
            ExternalTransfer::query()->whereIn('order_sn', $order_sns)->update(['is_reconciliation'=>1,'is_reconciliation_time'=>null]);
        }else{
            CarOrder::query()->whereIn('order_sn', $order_sns)->update(['is_reconciliation'=>1,'is_reconciliation_time'=>null]);
        }
        if($request->filled('id')){
            $data = $model->find($request->id);
            $data->deleted_at = now();
            $data->save();
        }
        return $this->success('修改成功');
    }
}
